<?php
/*************
#后台默认页面
#lsj 2010-11-13
*************/

class sysindexCtrl extends lib_ctrl
{
    public $Cname = '欢迎光临';
    protected $menu = null;
    protected $user = null;
    protected $group = null;
    protected $nowPowers = null;
    protected $nowMenuId = null;
    protected $nowGid = null;

    /*
    * 初始化
    */
    public function _init(){

    }

    /*
    *默认主页
    */
    public function indexAct()
    {
    	$this->view->display();
    }

    /*
	*权限验证
	*/
	public function authAct()
	{
		$ctrls = $this->getLastCtrlName();
		$acts = $this->getLastActionName();

		#异常报错
		if(!$ctrls || !$acts){
			$this->setParam(array('msg'=>'页面出错啦……'));
			$this->forward('index', 'error');
		}else{
			$nowCtrl = end($ctrls);
			$nowAct = end($acts);
		}

		#载入权限验证所需模型
		$this->menu = $this->loadModel('menu');
		$this->user = $this->loadModel('user');
		$this->group = $this->loadModel('group');

		#访问的地址不存在
		$this->nowMenuId = $this->menu->getId($nowCtrl, $nowAct);#当前菜单的ID， 一个或多个ID
		if(!$this->nowMenuId) {
			$this->forward('', 'error', array('msg'=>'访问的地址不存在'));
			exit;
		}

		#公共且不用登录的菜单，直接加载
		foreach($this->nowMenuId as $key=>$val){
			if($this->menu->isOpen($val)){
				//$this->loadMenus();#一般不用登录的菜单,是不用加载所有菜单项的
				$this->forward($nowCtrl, $nowAct);
				exit;
			}
		}

        #权限未设定
		$this->nowGid = $this->user->getNowGid();#当前用户所有组ID
		$this->nowGid = 1;
		$this->nowPowers = explode(',', $this->group->getPower($this->nowGid));#当前用户组权限列表，并数组化组权限
		if(!$this->nowPowers){
			$this->forward('', 'error', array('msg'=>'您的权限未设定'));
			exit;
		}

		#验证是否已登录
		if(!$this->user->isLogin()){
			$this->setParam(array('backurl'=>$this->getUrl($nowCtrl, $nowAct)));
			$this->forward('sysuser', 'login');
			exit;
		}

		#验证是否拥有当前所请求菜单的权限
		if(!$this->group->isDeve($this->nowGid) || ($this->group->isAdmin($this->nowGid) && !$this->menu->isAdmin($this->nowMenuId))){#开发人员，每个菜单均可用
			if(!array_intersect($this->nowMenuId, $this->nowPowers)){
				$this->setParam(array('msg'=>'您的权限不足'));
				$this->forward('', 'error');#权限不足
				exit;
			}
		}

		#定向到所访问的控制器的对应动作
		$this->loadMenus();#加载对应权限的菜单
 	       $pm['THEGLOBALS']['Uname'] = $this->user->getNowUname();
	       $this->setParam($pm);
		$this->forward($nowCtrl, $nowAct);
		exit;
	}

	/*
	* 加载菜单项, 头部总菜单 左侧菜单 右侧菜单 右侧当前位置
	*/
	public function loadMenus()
	{
		if(null === $this->menu) $this->menu = $this->loadModel('menu');
		if(null === $this->user) $this->user = $this->loadModel('user');
		if(null === $this->group) $this->group = $this->loadModel('group');

		if(null === $this->nowGid) $this->nowGid = $this->user->getNowGid();#当前用户所有组ID
		if(null === $this->nowMenuId) $this->nowMenuId = $this->menu->getId(end($this->getLastCtrlName()), end($this->getLastActionName()));#当前菜单ID
		if(null === $this->nowPowers) $this->nowPowers = explode(',', $this->group->getPower($this->nowGid));#当前用户组权限列表，并数组化组权限

		$param['THEGLOBALS'] = $this->getParam('THEGLOBALS');
		if(!isset($param['THEGLOBALS']) || !is_array($param['THEGLOBALS']))
		{
			$param['THEGLOBALS'] = array();#将菜单信息放入到解析到模板的公共参数里
			$menus = $this->menu->getInfo();#取出缓存的所有菜单信息

			#取出当前菜单的所有父级信息
			$fathers = array();
			$fathersNum = 0;
			foreach($this->nowMenuId as $key=>$val){
				$fat = $this->menu->getFather($val);
				if(count($fat) > $fathersNum) $fathers = $fat;
			}

			if($fathers){
				$fathersId = array();
				foreach($fathers as $key=>$val){
					$fathersId[] = $val['id'];
				}
			}

			$param['THEGLOBALS']['fathers'] = $fathers;
			$param['THEGLOBALS']['fids'] = $fathersId;

			foreach($menus as $key=>$val){
				if($this->menu->isHide($key)) continue;
				if(($this->group->isDeve($this->nowGid) || ($this->group->isAdmin($this->nowGid) && $this->menu->isAdmin($key)) || in_array($key, $this->nowPowers))){
					if($val['fid'] == 0) $param['THEGLOBALS']['topmenus'][$key] = $val;#第一级菜单
					if(isset($fathers[1]['id']) && $val['fid'] == $fathers[0]['id']) $param['THEGLOBALS']['leftmenus1'][$key] = $val;#第二级菜单
					if(isset($fathers[2]['id']) && $this->menu->getFid($val['fid']) == $fathers[0]['id']) $param['THEGLOBALS']['leftmenus2'][$val['fid']][$key] = $val;#第三级菜单
					if(isset($fathers[2]['id']) && $val['fid'] == $fathers[2]['id']) $param['THEGLOBALS']['rightmenus'][$key] = $val;#第四级菜单
				}
			}

		}
		//print_r($param['THEGLOBALS']);exit;
		$this->setParam($param);
		return $param['THEGLOBALS'];
	}

    /*
    *信息提示
    */
    public function errorAct()
    {
		$error = strip_tags(trim($this->getParam('msg')));
        $hideLink = abs(intval($this->getParam('link')));

		if(!$error) $error = '您的权限不够';

		$this->view->assign('error', $error);
        $this->view->assign('hideLink', $hideLink);
		$this->view->display();
    }

    /*
    *显示验证码
    */
    public function captchaAct()
    {
        $width = abs(intval($_REQUEST['w']));#宽
        $height = abs(intval($_REQUEST['h']));#高
        $line = abs(intval($_REQUEST['l']));#线条密度
        $shadow = abs(intval($_REQUEST['s']));#是否开启阴影
        $num = abs(intval($_REQUEST['num']));#显示的字符数
        $char = abs(intval($_REQUEST['char']));#是否显示字母，默认显示数字
        $sname = strip_tags(trim($_REQUEST['sn']));#SESSION变量的名字

        if(!$width) $width = 90;
        if(!$height) $height = 30;
        if(!$line) $line = 30;
        if(!$num) $num = 4;
        if($char) $char = 'a-z';
        else $char = '0-9';
        if(!$sname) $sname = 'captcha';

        session_start();

        $captcha = new lib_class_captcha;
        $captcha->SetCharSet($char);//设置字符的区间
        $captcha->SetNumChars($num);//设置显示字符数
        $captcha->SetWidth($width);//图片宽度
        $captcha->SetHeight($height);//图片宽度
        $captcha->UseColour(true); //彩色线条
        if($shadow) $captcha->DisplayShadow(true); //字符阴影
        $captcha->SetNumLines($line);//设置背景线条密度
        $captcha->SetMinFontSize(12);//最低字符大小
        $captcha->SetMaxFontSize(16);//最高字符大小
        $captcha->SetFileType('jpeg');
        $captcha->Create();
        $code = $captcha->GetCode();
        $_SESSION[$sname] = $code;
    }

    /*
    #检查验证码是否正确
    */
    function checkcaptchaAct()
    {
        session_start();
        $sname = strip_tags(trim($_POST['sname']));
        $captcha = strip_tags(trim($_POST['captcha']));
        if(!$sname) $sname = 'captcha';
        if(!$captcha) exit('0');#空数据
        if($captcha && $_SESSION[$sname] == $captcha) exit('1');#正确
        exit('-1');#错误
    }



}












?>
